package com.qgs.waf.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class CollectionUtil {
	/**
	 * @show 将数据为Map<byte[], byte[]> 转换为Map<String, String>
	 * @param datas
	 *            Map<byte[], byte[]>
	 * @return Map<String, String>
	 */
	public static Map<String, String> mapInByte2MapInObject(Map<byte[], byte[]> datas) {
		if (null == datas)
			return null;
		Map<String, String> values = new HashMap<String, String>();
		byte[] value;// 临时存储变量
		Set<byte[]> set = datas.keySet();
		Iterator<byte[]> iter = set.iterator();
		while (iter.hasNext()) {
			value = iter.next();
			values.put(new String(value), new String(datas.get(value)));
		}
		return values;
	}

	/**
	 * @show 将List<byte[]>数据转换为List<String>
	 * @param datas
	 *            List<byte[]> 需要转换的数据
	 * @return List<String>
	 */
	public static List<String> listInByte2String(List<byte[]> datas) {
		if (null == datas)
			return null;
		List<String> values = new LinkedList<String>();
		for (byte[] bs : datas) {
			values.add(new String(bs));
		}
		return values;
	}

	/**
	 * 获取Map中所有的Key
	 * 
	 * @param map
	 *            Map<T, K> map
	 * @return Set<T>
	 */
	public static <T, K> Set<T> getKeysInMap(Map<T, K> map) {
		return map.keySet();
	}

	/**
	 * @show 获取所有的Value
	 * @param map
	 *            Map<T, K> map
	 * @return List<K>
	 */
	public static <T, K> List<K> getValsInMap(Map<T, K> map) {
		List<K> vals = new LinkedList<K>();
		Set<T> keys = getKeysInMap(map);
		for (T t : keys) {
			vals.add(map.get(t));
		}
		return vals;
	}

	/**
	 * @show 获取所有的Value
	 * @param map
	 *            Map<T, K> map
	 * @return Map<T, K> map
	 */
	public static <T, K> Set<K> valsInMap(Map<T, K> map) {
		Set<K> vals = new TreeSet<K>();
		Set<T> keys = getKeysInMap(map);
		for (T t : keys) {
			vals.add(map.get(t));
		}
		return vals;
	}

	public static <T> TreeSet<T> collection2TreeSet(T[]... t) {
		TreeSet<T> re_ts = new TreeSet<T>();
		for (T[] ts : t) {
			for (T t2 : ts) {
				re_ts.add(t2);
			}
		}
		return re_ts;
	}

	/**
	 * @show 将byte数据转换为字符串数据
	 * @param bs
	 *            byte[]...
	 * @return String[]
	 */
	public static String[] byteArray2StringArray(byte[]... bs) {
		String[] vals = new String[bs.length];
		for (int i = 0; i < bs.length; i++) {
			vals[i] = new String(bs[i]);
		}
		return vals;
	}

	public static String[] treeSet2Array(TreeSet<String> ss) {
		ArrayList<String> als = new ArrayList<String>();
		Iterator<String> iter = ss.iterator();
		while (iter.hasNext()) {
			als.add(iter.next());
		}
		String[] tt = new String[ss.size()];
		for (int i = 0; i < als.size(); i++) {
			tt[i] = als.get(i);
		}
		return tt;
	}

	public static void main(String[] args) {
		Map<byte[], byte[]> datas = new HashMap<byte[], byte[]>();
		datas.put("key".getBytes(), "value".getBytes());
		System.out.println(mapInByte2MapInObject(datas));
	}

}
